package com.xl.rentkeeper;

import android.app.ActivityManager;
import android.app.AlarmManager;
import android.app.Application;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.SystemClock;
import android.support.v7.app.NotificationCompat;
import android.view.Gravity;
import android.widget.Toast;

import com.squareup.wire.Wire;
import com.xl.rentkeeper.business.SecurityLogic;
import com.xl.rentkeeper.business.StartupDirector;
import com.xl.rentkeeper.business.UserLogic;
import com.xl.rentkeeper.log.QLog;
import com.xl.rentkeeper.net.NetworkManager;
import com.xl.rentkeeper.receiver.KeepAliveReceiver;

import java.util.List;

/**
 * Created by zjm on 2015/6/24.
 */
public class App extends Application {
    public static App sApp;

    Wire wire = new Wire();
    public static long hbTime = 5*60*1000;
    @Override
    public void onCreate() {
        sApp = this;
        super.onCreate();
        QLog.init(this);
        UserLogic.getInstance();
        SecurityLogic.getInstance();

        App.addAlarm(SystemClock.elapsedRealtime() + 1000);
        App.registerScreenOnReceiver();

        startService();
        StartupDirector.getInstance().onAppCreate();

        NetworkManager.getInstance();
    }
    public static Wire getWire() {
        return getApp().wire;
    }
    public static App getApp() {
        return sApp;
    }

    public boolean isForeground() {
        if (getApp() == null) {
            return false;
        }
        ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager
                .getRunningAppProcesses();
        for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
            if (appProcess.processName.equals(getPackageName())) {
                if (appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
                    return true;
                }else{
                    return false;
                }
            }
        }
        return false;
    }
    //存储全局信息的sp
    public SharedPreferences getSp() {
        return this.getSharedPreferences("global", Context.MODE_PRIVATE);
    }


    public static void showToast(String strToast) {
        Toast toast =Toast.makeText(sApp, strToast, Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.CENTER,0,0);
        toast.show();
    }

    public static void showToast(int strid) {
        Toast toast =Toast.makeText(sApp, strid, Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.CENTER,0,0);
        toast.show();
    }

    public static void addAlarm(long time){
        AlarmManager am = (AlarmManager) sApp.getApplicationContext().getSystemService(Context.ALARM_SERVICE);
        Intent intent =new Intent(sApp, KeepAliveReceiver.class);
        intent.setAction(KeepAliveReceiver.action_alarm_keepalive);
        PendingIntent sender = PendingIntent.getBroadcast(sApp, 100012, intent, PendingIntent.FLAG_CANCEL_CURRENT);
//        long firstime= SystemClock.elapsedRealtime();
//        am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstime, 5 * 60ad * 1000, sender);
        if(Build.VERSION.SDK_INT<19){
            am.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, time, sender);
        }else{
            am.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, time, sender);
        }
    }

    public static void registerScreenOnReceiver(){
        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_SCREEN_ON);
        sApp.registerReceiver(new KeepAliveReceiver(), filter);
    }

    void startService() {
        Intent in = new Intent(this,CoreService.class);
        startService(in);
    }


    public void showNotification(String title,String msg,int id,Intent intent) {
        NotificationCompat.Builder builder = new NotificationCompat.Builder(
                App.getApp());
        builder.setAutoCancel(true);
        builder.setContentTitle(title);
        builder.setContentText(msg);
        builder.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE);
        builder.setSmallIcon(R.mipmap.ic_launcher);
        PendingIntent pendingIntent = PendingIntent.getActivity(App.getApp(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        builder.setContentIntent(pendingIntent);
        NotificationManager mNotificationManager = (NotificationManager) App.getApp().getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(id, builder.build());
    }
}
